//
//  InSoundTableViewController.m
//  Inner  Voice
//
//  Created by 徐海宇 on 16/3/3.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InSoundTableViewController.h"

#import "InSoundHeaderView.h"

#import "InSoundFavouriteTool.h"

#import "InSoundTableViewCell.h"

#import "InLoginViewController.h"

#import "InSound.h"

#import "InSoundDetail.h"

#import "InSoundFootView.h"

#import "downLoad.h"

#define kIdentifier @"statusCell"

#define screenWidth [UIScreen mainScreen].bounds.size.width

#define screenHeight [UIScreen mainScreen].bounds.size.height

#import "InSoundedFrame.h"

@interface InSoundTableViewController ()<downLoadSoundDelegate,InSoundHeaderDelegate>

@property (nonatomic,strong)InSoundDetail *soundDetail;

@property (nonatomic,strong)InSound *insound;

@property (nonatomic,copy)NSString *sound_creater_id;

@property (nonatomic,copy)NSString *sound_channel_id;

@property (strong,nonatomic)NSMutableArray *InSoundDetailArray;//模型text尺寸的数组

@end

@implementation InSoundTableViewController

- (NSMutableArray *)InSoundDetailArray {
    
    if (_InSoundDetailArray == nil) {
        
        InSoundedFrame *soundFrame = [[InSoundedFrame alloc]init];
        
        self.soundDetail.sound_info = self.dic[@"info"];
        
        soundFrame.soundDetail = self.soundDetail;
        //一定要弄第三人类型的数组来接受  我的理解是 懒加载相当于弱引用 _没用强引用
        NSMutableArray *soundDetailArray = [NSMutableArray array];
        
        [soundDetailArray addObject:soundFrame];
        
        _InSoundDetailArray = soundDetailArray;
        
    }
    return _InSoundDetailArray;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"歌曲详情";
    
    self.sound_creater_id = self.dic[@"user_id"];
    
    self.sound_channel_id = self.dic[@"channel_id"];
    //此方法可以封装
    self.soundDetail = [self loadInSoundDetailWithDic:self.dic];
    
    [self loadSoundCreater];
    
    [self loadSoundChannel];
    
    [self.tableView registerClass:[InSoundTableViewCell class] forCellReuseIdentifier:kIdentifier];
    
}


//代理方法
- (void)downLoad {
    
    NSLog(@" 歌曲信息 soundURL  %@",self.insound.inSound_Url);
    downLoad *down = [[downLoad alloc]init];
    
    [down downLoadWithInSound:self.insound];

}

- (void)showAlertView {
    
    [self alertViewShow];

}


- (void)concernBtn {

    [self alertViewShow];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //给出尺寸数组的元素个数
    return self.InSoundDetailArray.count;
}

//自定义label
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    InSoundTableViewCell *soundCell = [InSoundTableViewCell statusCellWithTaleView:tableView WithIndexPath:indexPath];
    
    soundCell.inSoundedFrame = self.InSoundDetailArray[indexPath.row];
    
    return soundCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    InSoundedFrame *friendFrame = self.InSoundDetailArray[indexPath.row];
    return friendFrame.cellHeight;
}
#pragma mark -加载head
- (void)loadHeardView{
    
    InSoundHeaderView *soundHeaderView = [[InSoundHeaderView alloc]init];
    
    soundHeaderView.frame = CGRectMake(0, 0, screenWidth, screenHeight/2);
    
    soundHeaderView.soundDetail = self.soundDetail;
    
    soundHeaderView.delegate = self;
    
    //    NSLog(@"%@",self.soundDetail.sound_like_count);
    
    self.tableView.tableHeaderView = soundHeaderView;
    
}
#pragma mark -加载foot
- (void)loadFooterView {
    
    InSoundFootView *soundFooterView = [[InSoundFootView alloc]init];
    
    soundFooterView.soundDetail = self.soundDetail;
    
    soundFooterView.delegate = self;
    
    self.tableView.tableFooterView = soundFooterView;
    
}

- (void)loadSoundCreater {
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    NSString *url = [NSString stringWithFormat:@"http://1.wangtest11.applinzi.com/home/Banner/backUserInfo?userid=%@",self.sound_creater_id];
    
    AFHTTPSessionManager *httpManager = [AFHTTPSessionManager manager];
    
    [httpManager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *responseArray = responseObject[@"data"];
        
        //用户信息  在请求后加载headerView
        self.soundDetail.sound_creater_id = self.sound_creater_id;
        
        self.soundDetail.sound_creater_icon = responseArray[0][@"photo"];
        
        self.soundDetail.sound_channel_name = responseArray[0][@"name"];
        
        [self loadHeardView];
        NSLog(@"歌曲信息 SUSSESS");
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"歌曲信息 %@",error);
    }];}
}

//根据上一个页面传过来的soundid得到对应的频道
- (void)loadSoundChannel {
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    NSString *url = [NSString stringWithFormat:@"http://1.wangtest11.applinzi.com/home/Banner/backchannelRequest?channelid=%@",self.sound_channel_id];
    
    AFHTTPSessionManager *httpManager = [AFHTTPSessionManager manager];
    
    [httpManager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *responseArray = responseObject[@"data"];
        
        self.soundDetail.sound_channel_icon = responseArray[0][@"pic_100"];
        
        self.soundDetail.sound_channel_name = responseArray[0][@"name"];
        
        [self loadFooterView];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
    }];
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
    
}
#pragma mark - 封装UIAlertView方法，直接调用就可以显示提示
-(void)showAlertView:(NSString *)string {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
}
//根据上一个页面得到字典
- (InSoundDetail *)loadInSoundDetailWithDic:(NSDictionary *)dic {
    
    InSoundDetail *soundDetail = [[InSoundDetail alloc]init];
    
    soundDetail.sound_info = self.dic[@"info"];
    
    soundDetail.sound_comment_count = self.dic[@"comment_count"];
    
    soundDetail.sound_download_count = self.dic[@"download_count"];
    
    soundDetail.sound_like_count = self.dic[@"like_count"];
    
    soundDetail.sound_picture = self.dic[@"pic"];
    
    soundDetail.sound_share_count = self.dic[@"share_count"];
    
    soundDetail.sound_name = self.dic[@"name"];
    
    soundDetail.sound_icon = self.dic[@"pic_100"];
    
    soundDetail.sound_id = self.dic[@"id"];
    
    soundDetail.sound_URL = self.dic[@"source"];
    
    InSound *insound = [[InSound alloc]init];
    
        insound.inSound_icon = soundDetail.sound_icon;
    
        insound.sound_id = soundDetail.sound_id;
    
        insound.inSound_name = soundDetail.sound_name;
    
        insound.inSound_Url = soundDetail.sound_URL;

        self.insound = insound;
    
    
    return soundDetail;
    
}


- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    if (((InSoundFootView *)self.tableView.tableFooterView).loveBtn.selected == YES) {
        [self loveNumberChange];
    }else {
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        
        NSString *user_id = [userDefault valueForKey:@"u_id"];
        
        [InSoundFavouriteTool deleteSound:self.soundDetail.sound_id WithUser_id:user_id];
        
    }


}

- (void)loveNumberChange {
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    AFHTTPSessionManager *httpSessionManager = [AFHTTPSessionManager manager];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    //要登陆后才能
    
    
 //   [userDefault setValue:@"11111" forKey:@"u_id"];
    
    NSString *userid = [userDefault valueForKey:@"u_id"];
    
    NSString *like_count = ((InSoundFootView *)self.tableView.tableFooterView).loveNumberLabel.text;
    
    NSString *url = [NSString stringWithFormat:@"http://1.wangtest11.applinzi.com/home/Banner/userFavouriteSounds?userid=%@&soundid=%@&like_count=%@",userid,self.soundDetail.sound_id,like_count];
    
    [httpSessionManager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"   success  ");
        
        [InSoundFavouriteTool insertSound:self.soundDetail.sound_id WithUser_id:userid];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
}





#pragma mark -alertView
- (void)alertViewShow {
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefaults valueForKey:@"u_id"];
    
    if (user_id == nil) {
       [alertView show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        InLoginViewController *loginController = [[InLoginViewController alloc]initWithNibName:@"InLoginViewController" bundle:nil];
        
        [self presentViewController:loginController animated:YES completion:nil];
        
    }
    
}



@end
